package com.kylin.module.community.orm;

import com.kylin.hibernate.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kylin.module.community.orm.Member;
import com.kylin.module.community.forms.MemberForm;
import com.kylin.module.user.orm.User;

import org.hibernate.Query;

/*
import org.hibernate.LockMode;
import org.hibernate.criterion.Example;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.exception.SystemException;
*/

/**
 * Data access object (DAO) for domain model class Testcompany.
 * @see com.kylin.module.test.orm.Testcompany
 * @author liangzh
 */
public class MemberDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(MemberDAO.class); 

    
	public Integer getTotal(MemberForm form){
	//public Long getTotal(){
        try {
            String queryString = "select count(*) from Member  where   active=1";        	   
            Query query = getSession().createQuery(queryString);
            log.info("member total: "+ query.list().get(0));
            //Long total = query.list().size();//query.list().get(0);
            //return new Integer(query.list().size());
            Long total = (Long)query.list().get(0);
   		 	//return (Integer)query.list().get(0);
   		 	return new Integer (total.intValue());
         } catch (RuntimeException re) {
            log.error("get member total", re);
            throw re;
         }		
	}
    
	public List getList(Integer limitstart, Integer limit, MemberForm form){
        log.info("finding member " + limitstart + " " + limit );
        try {
           String queryString = "from Member  where   active=1 order by  createdon desc";        	   
           Query query = getSession().createQuery(queryString);
           query.setFirstResult(limitstart.intValue());
           query.setMaxResults(limit.intValue());
  		 //query.setParameter(0, value);
           log.info("size: "+ query.list().size());
  		 return query.list();
        } catch (RuntimeException re) {
           log.error("find member by property name failed", re);
           throw re;
        }
		
	}  
    
    public List findLastReg() {
      log.info("finding last 10 register");
      try {
         String queryString = "from Member as m where  m.active=1 order by m.createdon desc";
         Query query = getSession().createQuery(queryString);
         query.setFirstResult(0);
         query.setMaxResults(10);
		 //query.setParameter(0, value);
         log.info("size: "+ query.list().size());
		 return query.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    
    public Member findme(User user) {
        log.info("finding me: id= " + user.getId());
        try {
           String queryString = "from Member as m where  m.user = ? and m.active=1 order by m.createdon desc";
           Query query = getSession().createQuery(queryString);
           query.setEntity(0, user);
           //query.setFirstResult(0);
           //query.setMaxResults(10);
  		 //query.setParameter(0, value);
           log.info("size: "+ query.list().size());
           if (query.list().size() != 0){
  		 	return (Member)query.list().get(0);
           }
           else{
        	   return null;
           }
  		 	
        } catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
        }
  	}
    
}
